package db

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	_ "github.com/mattn/go-sqlite3"
)

var Db *sqlx.DB
var DbCont *sqlx.DB
var DbLite *sqlx.DB

func init() {
	var err error
	//dsn := "root:123456@tcp(127.0.0.1:3306)/iray_proxy"
	dsn := "admin:admin@tcp(10.40.1.17:3306)/iray_proxy_20191210"
	Db, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		fmt.Printf("connect DB failed, err:%v\n", err)
		return
	}
	Db.SetMaxOpenConns(20)
	Db.SetMaxIdleConns(10)
}

func initCont() {
	var err error
	dsn := "admin:admin@tcp(10.40.1.17:3306)/iray_proxy_20191210"
	DbCont, err = sqlx.Connect("mysql", dsn)
	if err != nil {
		fmt.Printf("connect cont DB failed, err:%v\n", err)
		return
	}
	DbCont.SetMaxOpenConns(20)
	DbCont.SetMaxIdleConns(10)
}

func InitDbLite(path string) {
	var err error
	DbLite, err = sqlx.Connect("sqlite3", path)
	if err != nil {
		fmt.Printf("connect sqlite DB failed, err:%v\n", err)
		panic(err)
	}
	fmt.Println("init sqlite db: ", path)
}
